﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Think.Lenovo.Com.Models.Material;
using Think.Lenovo.Com.DataAccess;
using Think.Lenovo.Com.DataAccess.User;
using System.Data;


namespace Think.Lenovo.Com
{
    public partial class MaterialAdd : System.Web.UI.Page
    {
        MaterialBulletin model = new MaterialBulletin();

        MaterialInfo info = new MaterialInfo();
        UserInfo uinfo = new UserInfo();
        RegionInfo regioninfo = new RegionInfo();
        ReceiveMaterial RM = new ReceiveMaterial();
        ReceiveMaterialInfo RMinfo = new ReceiveMaterialInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ClassKind = Convert.ToInt32(Request.QueryString["ClassKind"]);
                BindRegion();
                BindClass();
            }
        }
        #region ClassKind属性
        protected int ClassKind
        {
            get
            {
                return (int)ViewState["ClassKind"];
            }
            set
            {
                ViewState["ClassKind"] = value;
            }
        }
        #endregion

        public void BindRegion()
        {
            cbl_region.DataSource = regioninfo.GetAllRegion();
            cbl_region.DataBind();
        }

        public void BindClass()
        {
            this.DropDownList1.Items.Clear();
            this.DropDownList1.Items.Add(new ListItem("添加为根栏目", "0"));
            DataSet ds = info.GetClassList(this.ClassKind);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int ClassTj = Convert.ToInt32(dr["ClassTj"]);
                string ClassId = dr["ClassId"].ToString().Trim();
                string ClassName = dr["ClassName"].ToString().Trim();

                if (ClassTj == 1)
                {
                    this.DropDownList1.Items.Add(new ListItem(ClassName, ClassId));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = Think.Lenovo.Com.productAdd.StringHelper.StringOfChar(ClassTj - 1, "　") + ClassName;

                    this.DropDownList1.Items.Add(new ListItem(ClassName, ClassId));
                }
            }
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            model.Title = TextBox1.Text;
            model.MaterialClass = Request["DropDownList1"];
            model.MaterialNum = Request["count"];
            model.Content = FCKeditor1.Value;
            model.ReleaseTime = DateTime.Now;

            long MaterialBulletin_Id = info.AddMaterial(model);

            if (MaterialBulletin_Id > 0)
            {
                //物料添加成功后,给选中的分区的每个分区管理员发送通知
                foreach (ListItem item in this.cbl_region.Items)
                {
                    if (item.Selected == true)
                    {
                        for (int j = 0; j < uinfo.GetUserIdByRegionLevelCount(2, Int32.Parse(item.Value)); j++)
                        {
                            RM.MaterialBulletin_Id = Int32.Parse(MaterialBulletin_Id.ToString());
                            RM.Receive = 0;
                            RM.UserId = Int32.Parse(uinfo.GetUserIdByRegionLevel(2, Int32.Parse(item.Value)).Tables[0].Rows[0][j].ToString());
                            RM.RegionId =Int32.Parse(item.Value);
                            RMinfo.AddReceiveMaterial(RM);
                        }
                    }
                }
                Response.Write("<script>alert('添加成功');location.href=location.href;</script>");
            }
        }
    }
}